<?php
// require 'ErrorCode.php';

class Article
{

	//数据库连接句柄
	private $db;

	/*
	*构造方法
	*PDO连接句柄
	*/
	public function __construct($_db)
	{
		$this->_db = $_db;
	}

	//创建文章
	public function create($title , $content , $userId)
	{
		if(empty($title)){
			throw new Exception('文章标题不能为空' , ErrorCode::ARTICLE_TITLE_CANNOT_EMPTY);
		}
		if(empty($content)){
			throw new Exception('文章内容不能为空' , ErrorCode::ARTICLE_CONTENT_CANNOT_EMPTY);
		}
		$sql = "INSERT INTO `article` (`title` , `content` , `userId` , `createdAt`) VALUES (:title , :content , :userId , :createdAt)";
		$createdAt = time();
		$stmt = $this->_db->prepare($sql);
		$stmt->bindParam(':title' , $title);
		$stmt->bindParam(':content' , $content);
		$stmt->bindParam(':userId' , $userId);
		$stmt->bindParam(':createdAt' , $createdAt);
		if(!$stmt->execute()){
			throw new Exception("发表文章失败", ErrorCode::ARTICLE_CREATE_FAIL);
		}
		return [
			'articleId'=>$this->_db->lastInsertId(),
			'title'=>$title,
			'content'=>$content,
			'userId'=>$userId,
			'createdAt'=>$createdAt
		];
	}

	//查看文章
	public function view($articleId)
	{
		if(empty($articleId)){
			throw new Exception('文章ID不能为空' , ErrorCode::ARTICLE_ID_CANNOT_EMPTY);
		}
		$sql = "SELECT * FROM `article` WHERE articleId=:articleId";
		$stmt = $this->_db->prepare($sql);
		$stmt->bindParam(':articleId' , $articleId);
		$stmt->execute();
		$article = $stmt->fetch(PDO::FETCH_ASSOC);
		if(empty($article)){
			throw new Exception('文章不存在' , ErrorCode::ARTICLE_NOT_FOUND);
		}
		return $article;
	}

	//编辑文章
	public function edit($articleId , $title , $content , $userId)
	{
		$article = $this->view($articleId);
		if($article['userId']!=$userId){
			throw new Exception("您无权编辑该文章", ErrorCode::PERMISSION_DEFINED);
		}
		$title = empty($title) ? $article['title'] : $title;					//如果title为空，则使用原标题，也就是说不修改title
		$content = empty($content) ? $article['content'] : $content;			//如果content为空，则使用原标题，也就是说不修改content

		if($title == $article['title'] && $content === $article['content']){
			return $article;
		}
		$sql = "UPDATE `article` SET `title`=:title,`content`=:content WHERE `articleId`=:articleId";
		$stmt = $this->_db->prepare($sql);
		
		$stmt->bindParam(':title' , $title);
		$stmt->bindParam(':content' , $content);
		$stmt->bindParam(':articleId' , $articleId);
		if(!$stmt->execute()){
			throw new Exception('文章编辑失败' , ErrorCode::ARTICLE_EDIT_FAIL);
		}
		return [
			'articleId'=>$articleId,
			'title' => $title , 
			'content'=>$content,
			'createdAt'=>$article['createdAt']
		];
	}

	//删除文章
	public function delete($articleId , $userId)
	{
		$article = $this->view($articleId);
		if(empty($article)){
			throw new Exception("文章不存在", ErrorCode::ARTICLE_NOT_FOUND);
		}
		$sql = "DELETE FROM `article` WHERE `articleId`=:id AND `userId`=:userId";
		$stmt = $this->_db->prepare($sql);
		$stmt->bindParam(':id' , $articleId);
		$stmt->bindParam(':userId' , $userId);
		if(!$stmt->execute()){
			throw new Exception("删除失败", ErrorCode::ARTICLE_DELETE_FAIL);
		}
		return true;
	}

	//读取文章列表
	public function getList($userId , $page = 1 , $size = 10)
	{
		
		if(empty($userId)){
			throw new Exception("用户ID不能为空", ErrorCode::ARTICLE_USERID_CANNOT_EMPTY);
		}
		if($size>100){
			throw new Exception("分页大小最大为100", ErrorCode::PAGE_SIZE_TO_BIG);
		}
		$p = ($page-1)*$size;
		$sql = "SELECT * FROM `article` WHERE `userId`=:userId LIMIT :limit,:offset";
		$stmt = $this->_db->prepare($sql);
		$stmt->bindParam(':userId',$userId);
		$stmt->bindParam(':limit',$p);
		$stmt->bindParam(':offset',$size);
		$stmt->execute();
		$articlelist = $stmt->fetchAll(PDO::FETCH_ASSOC);
	
		return $articlelist;
	} 
}